import axios from 'axios'
import vueRouter from '../route'
import util from './util'
import config from './config'

const code = config.config.code;
const userInfo = util.userInfo;

axios.defaults.timeout = 10 * 1000;
/**
 * 请求拦截器
 * 所有请求带有Body参数
 */
axios.interceptors.request.use(function (config) {
    config.data = {
        userphone: userInfo.userphone,
        token: userInfo.token,
        ...config.data,
    };
    return config
}, function (error) {
    return Promise.reject(error);
});

/**
 * 响应拦截器
 * 对响应失效进行处理
 */
axios.interceptors.response.use(function (response) {
    if (response.data.errcode === code["expError"]) {
        util.clearStroage();
        vueRouter.replace('/login')
    }
    return response
}, function (error) {
    return Promise.reject(error);
});

export default {
    util
}